package com.example.jeong.yuejian10.Activity;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.jeong.yuejian10.Bean.UserBean;
import com.example.jeong.yuejian10.Model.UserModel;
import com.example.jeong.yuejian10.R;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.jeong.yuejian10.constant.Constant.USER_PHOTO_URL;

//修改个人信息也
public class ModifyUserActivity extends AppCompatActivity {

    private EditText uphone;
    private EditText uname;
    private EditText upass;
    private Button modify_button;
    private ImageView upic;
    private ImageView left;


    private int u_id;
    private String u_name;
    private String u_pass;
    private String  u_phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user_info);
        initView();
        getUserDate();
        initEvent();
    }
    //修改个人信息
    public void initView(){
        uphone=(EditText)findViewById(R.id.u_phone);
        uname=(EditText)findViewById(R.id.u_name);
        upass=(EditText)findViewById(R.id.u_pass);
        upic=(ImageView)findViewById(R.id.u_pic);
        left=(ImageView)findViewById(R.id.left);
        modify_button=(Button)findViewById(R.id.modify_button);
    }
    public void getUserDate(){
        SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("u_id",0);
        // Log.d("userbg","u_id");

        UserModel userModel = new UserModel();
        Call call = userModel.edituser(u_id);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean user = response.body();
                uname.setText(user.getU_name());
                upass.setText(user.getU_pass());
                uphone.setText(user.getU_phone());
                // Log.d("userbg","username");
                String pic=USER_PHOTO_URL+user.getU_pic();
                Picasso.with(ModifyUserActivity.this).load(pic).into(upic);
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
            }
        });
    }
    public void initEvent(){
        left.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        modify_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                modifyDate();
            }
        });
    }

    private void modifyDate() {
        u_name = uname.getText().toString();
        u_pass = upass.getText().toString();
        u_phone = uphone.getText().toString();
        if (u_name.equals("")) {
            Toast.makeText(ModifyUserActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
            return;
        } else {
            UserModel userModel = new UserModel();
            Call call = userModel.updateuser(u_id, u_name, u_pass,u_phone);
            call.enqueue(new Callback<UserBean>() {
                @Override
                public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                    UserBean result = response.body();
                    String data = result.getSuccess();
                    System.out.print(data);
                    if (data.equals("1")) {
                        Toast.makeText(ModifyUserActivity.this, "修改成功！", Toast.LENGTH_LONG).show();
                        //Toast.makeText(MyInfoActivity.this, password, Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(ModifyUserActivity.this, "修改失败！", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<UserBean> call, Throwable t) {

                }
            });
        }
    }
}
